/*
 * Copyright (C) 2006-2007 Eskil Bylund
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

using System;

using Gtk;

using DCSharp.Backend.Connections;
using DCSharp.Backend.Objects;

namespace DCSharp.Gui
{
	public class UserStore : ListStore
	{
		public enum Column {
			User,
			Hub,
			Nick,
			ShareSize // For sorting only
		};

		public UserStore() : base(typeof(object), typeof(object),typeof(string))
		{
		}

		#region Methods

		public TreeIter AddUser(Identity user, HubConnection hub)
		{
			if (user == null)
			{
				throw new ArgumentNullException("user");
			}
			return AppendValues(user, hub, user.Nick);
		}

		public void RemoveUser(Identity user)
		{
			TreeIter iter;
			if (GetIter(user, out iter))
			{
				Remove(ref iter);
			}
		}

		public bool Contains(Identity user)
		{
			TreeIter iter;
			return GetIter(user, out iter);
		}

		public bool GetIter(Identity user, out TreeIter iter)
		{
			if (GetIterFirst(out iter))
			{
				do
				{
					Identity info = GetValue(iter, (int)Column.User) as Identity;
					if (info == user)
					{
						return true;
					}
				}
				while (IterNext(ref iter));
			}
			return false;
		}

		public HubConnection GetHub(Identity user)
		{
			TreeIter iter;
			if (GetIter(user, out iter))
			{
				 return GetValue(iter, (int)Column.Hub) as HubConnection;
			}
			return null;
		}

		public void SetHub(Identity user, HubConnection hub)
		{
			TreeIter iter;
			if (GetIter(user, out iter))
			{
				SetValue(iter, (int)Column.Hub, hub);
			}
		}

		#endregion
	}
}
